﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HRM.BLL;
using HRM.DAL.DAO;

namespace HRM.UI.HRManagerUI.Configuration
{
    public partial class AddStatus : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PopulateStatus();

            }
        }



        private void PopulateStatus()
        {
            try
            {
                StatusManager manager = new StatusManager();
                List<Status> status = new List<Status>();
                status = manager.GetAllStatus();
                EmpStatusGridview.DataSource = status;
                EmpStatusGridview.DataBind();

            }
            catch (Exception)
            {

                throw;
            }
        }

        protected void SaveStatus(object sender, EventArgs e)
        {
            try
            {
                Status aStatus = new Status();
                StatusManager manager = new StatusManager();
                aStatus.EmpStatus = txtAddempStatus.Value;
                if (manager.SaveStatus(aStatus))
                {
                    ClearInputs();
                    msgLabel.Text = "Status Saved Successfully";
                    msgLabel.ForeColor = Color.Green;

                }
                else
                {
                    msgLabel.Text = "Status Not saved";
                    msgLabel.ForeColor = Color.Red;
                }
                PopulateStatus();

            }
            catch (Exception)
            {

                throw;
            }

        }
        private void ClearInputs()
        {
            txtAddempStatus.Value = "";


        }

        protected void EmpStatusGridview_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            EmpStatusGridview.PageIndex = e.NewPageIndex;
            PopulateStatus();

        }

        protected void EmpStatusGridview_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            e.Cancel = true;
            EmpStatusGridview.EditIndex = -1;
            PopulateStatus();

        }

        protected void EmpStatusGridview_RowEditing(object sender, GridViewEditEventArgs e)
        {
            EmpStatusGridview.EditIndex = e.NewEditIndex;
            PopulateStatus();

        }

        protected void EmpStatusGridview_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            StatusManager manager = new StatusManager();
            Status status = new Status();
            int Id = Convert.ToInt32(((Label)EmpStatusGridview.Rows[e.RowIndex].FindControl("lblStatusId")).Text);
            string EmpStatus = ((TextBox)EmpStatusGridview.Rows[e.RowIndex].FindControl("txtEmpStatusEdit")).Text;
            if (manager.EditStatus(Id, EmpStatus))
            {
                msgLabel.Text = "Satatus Updated successfully";
                msgLabel.ForeColor = Color.Green;
                EmpStatusGridview.EditIndex = -1;
                PopulateStatus();

            }
            else
            {
                msgLabel.Text = "Sorry !, Satatus not  Updated ";
                msgLabel.ForeColor = Color.FromArgb(255, 255, 83, 55);

            }


        }

        protected void EmpStatusGridview_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            StatusManager manager = new StatusManager();
            Status aStatus = new Status();
            int statusId = Convert.ToInt32(((Label)EmpStatusGridview.Rows[e.RowIndex].FindControl("lblStatusId")).Text);
            if (manager.DeleteStatus(statusId))
            {
                msgLabel.Text = "Satatus deleted successfully";
                msgLabel.ForeColor = Color.Green;
                PopulateStatus();

            }
            else
            {
                msgLabel.Text = "Satatus not  deleted ";
                msgLabel.ForeColor = Color.FromArgb(255, 255, 83, 55);


            }
            PopulateStatus();


        }
    }
}